from typing import List

"""
定义一个同样的二维数组dp[i][j]表示到这个点的最短距离

公式： dp[i][j] = min(dp[i][j-1], dp[i-1][j]) + grid[i][j]
"""


class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        if not grid:
            return 0
        m = len(grid)
        n = len(grid[0])
        if n == 0:
            return 0

        dp = [[0] * n for i in range(m)]
        dp[0][0] = grid[0][0]
        print(dp)
        for i in range(m):
            for j in range(n):
                if j == 0 and i == 0:
                    continue
                if i == 0:  # 处理第一排
                    print(i, j, dp[i][j])
                    dp[i][j] = dp[i][j-1] + grid[i][j]
                elif j == 0:
                    dp[i][j] = dp[i-1][j] + grid[i][j]
                else:
                    dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]
        return dp[m-1][n-1]


if __name__ == "__main__":
    grid = [[1, 3, 1], [1, -5, 1], [4, 2, 1]]
    print(Solution().minPathSum(grid))
